-- 常用工具方法

-- 模块定义 
local M = {};
setmetatable (M, {__index = _G;});
_ENV = M;

-- 方法定义

-- 创建一个空表，并将其metatable.__index属性指向base_class_type.operation_table，以模拟方法继承
function CreateOperationTable (base_class_type)
    local operation_table = {};
    local mt = {__index = base_class_type.operation_table};
    setmetatable (operation_table, mt);
    return operation_table;
end;

-- 为instance创建metatable，并将其__index属性指向class_type.operation_table，以将instance绑定为class_type的实例对象
function PrepareInstance (class_type, instance)
    local mt = 
    {
        __index = class_type.operation_table,
        __tostring = function (self)
            return class_type.operation_table.__tostring (self)
        end
    };
    setmetatable (instance, mt);
    instance._class_type = class_type;
end;

-- 检查derived_type是不是ancestor_type的子类
function IsAncestorOf (ancestor_type, derived_type)
    local parent_class = derived_type._base_class;
    while parent_class ~= nil do
        if (ancestor_type == parent_class) then
            return true;
        end;
        parent_class = parent_class._base_class;
    end;
    
    return false;
end;

-- 获取类定义名称
function GetClassName (class_type)
    local pattern = "[%a]+$";
    return string.match (class_type._class_name, pattern);
end;

return M;
